package com.imesne.office.excel.model;

import com.imesne.assistant.common.validation.Verifier;
import com.imesne.office.excel.utils.ExcelKitUtils;

/**
 * Created by liyd on 17/7/4.
 */
public class ExcelRange {

    private Integer sheetNum;
    private Integer beginRowNum;
    private Integer endRowNum;
    private Integer beginCellNum;
    private Integer endCellNum;
    private Integer placeholderRow;

    public ExcelRange() {
        this.sheetNum = 1;
        this.beginRowNum = 1;
        this.endRowNum = Integer.MAX_VALUE;
        this.beginCellNum = 1;
        this.endCellNum = Integer.MAX_VALUE;
        placeholderRow = 0;
    }

    public Integer getSheetNum() {
        return sheetNum;
    }

    public Integer getBeginRowNum() {
        return beginRowNum;
    }

    public Integer getEndRowNum() {
        return endRowNum;
    }

    public Integer getBeginCellNum() {
        return beginCellNum;
    }

    public Integer getEndCellNum() {
        return endCellNum;
    }

    public Integer getPlaceholderRow() {
        return placeholderRow;
    }

    public static RangeBuilder builder() {
        return new RangeBuilder();
    }

    public static class RangeBuilder {

        /**
         * 程序中索引从0开始，减1
         */
        ExcelRange excelRange = new ExcelRange();

        public RangeBuilder sheetNum(int sheetNum) {
            this.excelRange.sheetNum = sheetNum;
            return this;
        }

        public RangeBuilder beginRowNum(int beginRowNum) {
            this.excelRange.beginRowNum = beginRowNum;
            return this;
        }

        public RangeBuilder endRowNum(int endRowNum) {
            this.excelRange.endRowNum = endRowNum;
            return this;
        }

        public RangeBuilder beginCellNum(int beginCellNum) {
            this.excelRange.beginCellNum = beginCellNum;
            return this;
        }

        public RangeBuilder beginCellNum(String beginCellNum) {
            int num = ExcelKitUtils.columnToNum(beginCellNum);
            this.excelRange.beginCellNum = num;
            return this;
        }

        public RangeBuilder endCellNum(int endCellNum) {
            this.excelRange.endCellNum = endCellNum;
            return this;
        }

        public RangeBuilder endCellNum(String endCellNum) {
            int num = ExcelKitUtils.columnToNum(endCellNum);
            this.excelRange.endCellNum = num;
            return this;
        }

        public RangeBuilder placeholderRow(int placeholderRow) {
            this.excelRange.placeholderRow = placeholderRow;
            return this;
        }

        public ExcelRange build() {
            Verifier.init()
                    .notNull(this.excelRange.sheetNum, "sheetNum")
                    .notNull(this.excelRange.beginRowNum, "beginRowNum")
                    .notNull(this.excelRange.endRowNum, "endRowNum")
                    .notNull(this.excelRange.beginCellNum, "beginCellNum")
                    .notNull(this.excelRange.endCellNum, "endCellNum")
                    .validate();
            return this.excelRange;
        }
    }
}
